name: Java Tests

on:
  workflow_call:
    inputs:
      safe-checkout:
        required: true
        description: "The SHA key for the commit we want to run over"
        type: string

jobs:
  linux:
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: OpenJDK 8
            version: '8'
            image: us-docker.pkg.dev/protobuf-build/containers/test/linux/java:8-03a376b5d6ef66f827fc307716e3b841cc26b709
            targets: //java/... //java/internal:java_version
          - name: OpenJDK 11
            version: '11'
            image: us-docker.pkg.dev/protobuf-build/containers/test/linux/java:11-03a376b5d6ef66f827fc307716e3b841cc26b709
            targets: //java/... //java/internal:java_version
          - name: OpenJDK 17
            version: '17'
            image: us-docker.pkg.dev/protobuf-build/containers/test/linux/java:17-03a376b5d6ef66f827fc307716e3b841cc26b709
            targets: //java/... //java/internal:java_version
          - name: aarch64
            version: 'aarch64'
            image: us-docker.pkg.dev/protobuf-build/containers/test/linux/emulation:aarch64-3af05275178e16af30961976af126eabbbb2c733
            targets: //java/... //src/google/protobuf/compiler:protoc_aarch64_test

    name: Linux ${{ matrix.name }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout pending changes
        uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
        with:
          ref: ${{ inputs.safe-checkout }}
      - name: Run tests
        uses: protocolbuffers/protobuf-ci/bazel-docker@v1
        with:
          image: ${{ matrix.image }}
          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
          bazel-cache: java_linux/${{ matrix.version }}
          bazel: test ${{ matrix.targets }} --test_env=KOKORO_JAVA_VERSION

  linkage-monitor:
    name: Linux Linkage Monitor
    runs-on: ubuntu-latest
    steps:
      - name: Checkout pending changes
        uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
        with:
          ref: ${{ inputs.safe-checkout }}
      - name: Build protoc
        id: build-protoc
        uses: protocolbuffers/protobuf-ci/cross-compile-protoc@v1
        with:
          image: us-docker.pkg.dev/protobuf-build/containers/common/linux/bazel:5.1.1-6361b3a6e5c97e9951d03a4de28542fc45f1adab
          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
          architecture: linux-x86_64
      - name: Move protoc into place and clean up
        run: |
          mv ${{ steps.build-protoc.outputs.protoc }} protoc
          sudo rm -rf _build
      - name: Install snapshot version locally
        run: |
          cd java
          mvn -e -B -Dhttps.protocols=TLSv1.2 install -Dmaven.test.skip=true
      - name: Download Linkage Monitor
        run: curl -v -O "https://storage.googleapis.com/cloud-opensource-java-linkage-monitor/linkage-monitor-latest-all-deps.jar"
      - name: Fails if there's new linkage errors compared with baseline
        run: java -Xmx4096m -jar linkage-monitor-latest-all-deps.jar com.google.cloud:libraries-bom

  protobuf-bom:
    name: Protobuf Maven BOM
    runs-on: ubuntu-latest
    steps:
    - name: Checkout pending changes
      uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
      with:
        ref: ${{ inputs.safe-checkout }}
    - name: Build protoc
      id: build-protoc
      uses: protocolbuffers/protobuf-ci/cross-compile-protoc@v1
      with:
        image: us-docker.pkg.dev/protobuf-build/containers/common/linux/bazel:5.1.1-6361b3a6e5c97e9951d03a4de28542fc45f1adab
        credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
        architecture: linux-x86_64
    - name: Move protoc into place and clean up
      run: |
        mv ${{ steps.build-protoc.outputs.protoc }} protoc
        sudo rm -rf _build
    - name: Install snapshot version locally (not using generated pom.xml)
      run: |
        mvn -e -B -Dhttps.protocols=TLSv1.2 install -Dmaven.test.skip=true
      working-directory: java
    - name: Generate pom.xml files from the template
      uses: protocolbuffers/protobuf-ci/bazel-docker@v1
      with:
        image: us-docker.pkg.dev/protobuf-build/containers/test/linux/java:11-03a376b5d6ef66f827fc307716e3b841cc26b709
        credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
        bazel-cache: java_linux/11
        # protobuf-java and protobuf-java-util are the member of the BOM
        bash: |
          bazel build //java/core:core_mvn-pom //java/util:util_mvn-pom
          cp bazel-bin/java/core/core_mvn-pom.xml .
          cp bazel-bin/java/util/util_mvn-pom.xml .
    - name: Copy the generated pom.xml files to the local Maven repository
      shell: bash
      run: |
        LOCAL_MAVEN_GROUP_DIR="${HOME}/.m2/repository/com/google/protobuf"
        VERSION=$(grep "<version>" core_mvn-pom.xml | sed "s/<version>\(.*\)<\/version>/\1/" | xargs)
        cp core_mvn-pom.xml ${LOCAL_MAVEN_GROUP_DIR}/protobuf-java/${VERSION}/protobuf-java-${VERSION}.pom
        cp util_mvn-pom.xml ${LOCAL_MAVEN_GROUP_DIR}/protobuf-java-util/${VERSION}/protobuf-java-util-${VERSION}.pom
    - name: Clean up
      run: |
        sudo rm -rf _build
    - name: Validate Protobuf BOM
      uses: googleapis/java-cloud-bom/tests/validate-bom@v26.13.0
      with:
        bom-path: java/bom/pom.xml
